package com.hxzy.tms.interceptor;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 时间记录器
 * @author 
 *
 */
public class AppTimerInterceptor extends AbstractInterceptor {
	private  final Logger logger = LoggerFactory.getLogger(getClass());
	
	private static final long serialVersionUID = 6702442071245780909L;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// 執行 action 之前的預处理
		long startTime = System.currentTimeMillis(); 

		// 真正的去执行 action
		String actionResult = invocation.invoke();
		invocation.getAction();
		
		// 执行 action 之后的后处理
		long execTime = System.currentTimeMillis() - startTime;
		
		logger.debug("{} 请求耗时 {}", ServletActionContext.getRequest().getRequestURI(), execTime);
		return actionResult;
	}

}
